package com.eclink.asset.hardware.controller;

import com.eclink.asset.domain.query.WarehouseQuery;
import com.eclink.asset.hardware.domain.dao.Device;
import com.eclink.asset.hardware.domain.dto.DeviceDetailDTO;
import com.eclink.asset.hardware.service.DeviceService;
import com.eclink.asset.hardware.service.RfidService;
import com.eclink.asset.hardware.service.HardwareService;
import com.ruoyi.common.core.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设备Controller
 * 
 * @author chenshixian
 * @date 2024-11-16
 */
@Api(tags="设备管理接口")
@RestController
@RequestMapping("/device")
public class DeviceController extends BaseController {

    @Resource
    private DeviceService service;

    @Resource
    private RfidService rfidService;

    @ApiOperation("查询")
    @PostMapping("/list")
    public ResponseEntity<List<Device>> list() {
        List<Device> list = service.selectList();
        for(Device item : list) {
            Integer status = rfidService.getStatus(item.getNumber());
            item.setStatus(status);
        }
        return ResponseEntity.ok(list);
    }

    @ApiOperation("查询我的设备")
    @GetMapping("/listMyDevice")
    public ResponseEntity<List<Device>> listMyDevice() {
        List<Device> list = service.selectMyList();
        for(Device item : list) {
            Integer status = rfidService.getStatus(item.getNumber());
            item.setStatus(status);
        }
        return ResponseEntity.ok(list);
    }

    @ApiOperation("更新我的设备")
    @PutMapping("/updateMyDevice/{ids}")
    public ResponseEntity<Integer> updateMyDevice(@PathVariable Long[] ids) {
        service.updateMyDevice(ids);
        return ResponseEntity.ok(1);
    }

    @ApiOperation("删除我的设备")
    @DeleteMapping("/deleteMyDevice/{ids}")
    public ResponseEntity<Integer> deleteMyDevice(@PathVariable Long[] ids) {
        service.deleteMyDevice(ids);
        return ResponseEntity.ok(1);
    }

    @ApiOperation("获取详细信息")
    @GetMapping(value = "/{id}")
    public ResponseEntity<Device> getInfo(@PathVariable("id") Long id) {
        return ResponseEntity.ok(service.selectById(id));
    }

    @ApiOperation("新增")
    @PostMapping
    public ResponseEntity<Integer> add(@RequestBody Device entity) {
        return ResponseEntity.ok(service.insert(entity));
    }

    @ApiOperation("修改")
    @PutMapping
    public ResponseEntity<Integer> edit(@RequestBody Device entity) {
        return ResponseEntity.ok(service.update(entity));
    }

    @ApiOperation("删除")
	@DeleteMapping("/{ids}")
    public ResponseEntity<Integer> remove(@PathVariable Long[] ids) {
        service.deleteByIds(ids);
        return ResponseEntity.ok(1);
    }

    @ApiOperation("启动")
    @PutMapping("/start/{id}")
    public ResponseEntity<Integer> start(@PathVariable Long id) {
        service.start(id);
        return ResponseEntity.ok(1);
    }

    @ApiOperation("启动")
    @PutMapping("/shutdown/{id}")
    public ResponseEntity<Integer> shutdown(@PathVariable Long id) {
        service.shutdown(id);
        return ResponseEntity.ok(1);
    }

    @ApiOperation("获取消息列表")
    @GetMapping(value = "/getDeviceDetail/{id}")
    public ResponseEntity<DeviceDetailDTO> getDeviceDetail(@PathVariable Long id) {
        DeviceDetailDTO dto = rfidService.getDetail(id);
        return ResponseEntity.ok(dto);
    }

}
